package com.leagsoft.emm.demo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huawei.secoclient.sslvpn.ConstValue;
import com.huawei.secoclient.sslvpn.VpnUtils;
import com.huawei.secoclient.sslvpn.entity.LoginEntity;
import com.leagsoft.emm.demo.ui.ClearEditText;
import com.leagsoft.emm.demo.util.ContentUriUtil;
import com.leagsoft.emm.demo.util.ToastUtil;

/**
 * 证书相关登陆界面
 */
public class NameAndCertLoginActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String TAG = "NameAndCertLoginActivity";
    private ClearEditText etUserName;
    private ClearEditText etPassword;
    private LinearLayout llCertPassword;
    private LinearLayout llCertUserName;
    private TextView tvCert;
    private RelativeLayout rlCert;

    private int cauthType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name_cert_login);
        cauthType = getIntent().getIntExtra("authMode", 1);
        initView();
    }

    private void initView() {
        etPassword = (ClearEditText) findViewById(R.id.et_password);
        etUserName = (ClearEditText) findViewById(R.id.et_user_name);
        llCertUserName = findViewById(R.id.ll_cert_user);
        llCertPassword = findViewById(R.id.ll_cert_password);

        tvCert = (TextView) findViewById(R.id.tv_cert_choose);
        rlCert = (RelativeLayout) findViewById(R.id.rl_cert_choose);

        rlCert.setOnClickListener(this);
        etPassword.setLongClickable(false);
        etPassword.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }
        });

        if (cauthType == 1) {
            llCertUserName.setVisibility(View.GONE);
            llCertPassword.setVisibility(View.GONE);
        } else if (cauthType == 2) {
            llCertUserName.setVisibility(View.GONE);
            llCertPassword.setVisibility(View.VISIBLE);
        } else {
            llCertUserName.setVisibility(View.VISIBLE);
            llCertPassword.setVisibility(View.VISIBLE);
        }
        findViewById(R.id.bt_login).setOnClickListener(this);
    }

    //开始登录
    private void login() {
        final String userName = etUserName.getText().toString().trim();
        final String password = etPassword.getText().toString().trim();
        LoginEntity loginEntitycert = new LoginEntity(cauthType);
        loginEntitycert.setCertName(tvCert.getText().toString());
//        loginEntitycert.setCertName("/sdcard/test1/ltime.p12");
//        loginEntitycert.setCertPassword("1234567");//本地证书自身密码
        if (cauthType >= 2) {
            loginEntitycert.setPassword(password);
        }
        if (cauthType == 3) {
            loginEntitycert.setUserName(userName);
        }
        boolean isLogin = VpnUtils.startLogin(loginEntitycert, code -> {
            Log.i(TAG, "login callback:" + code);
            if (code == ConstValue.LOGIN_SUCCESS) {
                ToastUtil.show(NameAndCertLoginActivity.this, "登录成功 响应码:" + code);
                finish();
            } else if (code == ConstValue.LOGIN_VERIFY_SERVER) {
                ToastUtil.show(NameAndCertLoginActivity.this, "本地服务器验证失败 响应码:" + code);
            } else if (code == ConstValue.LOGIN_NEED_MODIFY_PASSWORD) {
                startActivity(new Intent(NameAndCertLoginActivity.this, ModifyPasswordActivity.class));
                ToastUtil.show(NameAndCertLoginActivity.this, "需要修改密码 响应码:" + code);
                finish();
            } else if (code == ConstValue.LOGIN_NEED_SEND_VERIFICATION_CODE) {
                startActivity(new Intent(NameAndCertLoginActivity.this, SimWarningCheckActivity.class));
                ToastUtil.show(NameAndCertLoginActivity.this, "需要进行双因子验证 响应码:" + code);
                finish();
            } else {
                ToastUtil.show(NameAndCertLoginActivity.this, "登录失败 响应码:" + code);
            }
        });
        if (isLogin) {
            ToastUtil.show(NameAndCertLoginActivity.this, "登录构建成功");
        } else {
            ToastUtil.show(NameAndCertLoginActivity.this, "登录构建失败");
        }
    }


    private void chooseFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 9999);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 9999) {
            Uri uri = data.getData();
            if (uri != null) {
                tvCert.setText(ContentUriUtil.getPath(this, uri));
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                login();
                break;
            case R.id.rl_cert_choose:
                chooseFile();
                break;
        }
    }
}
